package com.example.mvp_demo.view;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.mvp_demo.R;
import com.example.mvp_demo.data.Phone;
import com.example.mvp_demo.moudle.PhoneFactory;
import com.example.mvp_demo.presenter.PhonePresenter;

import java.util.Random;

//TODO view 层,实现处理view层业务逻辑的接口,重写方法
public class MainActivity extends AppCompatActivity implements OperationView{

    private PhonePresenter mPhonePresenter;
    private ListView mListView;
    private ArrayAdapter<Phone> mArrayAdapter;
    private ProgressDialog mProgressDialog;
    private Toast mToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        //创建手机工厂对象并创建一个手机  TODO M层
        PhoneFactory phoneFactory = new PhoneFactory();
        phoneFactory.createPhone("nokia",555);

        //创建Presenter对象, TODO P层
        mPhonePresenter = new PhonePresenter(phoneFactory,this);

        //找到listview,创建适配器
        mListView = (ListView) findViewById(R.id.listview);
        mArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, phoneFactory.getPhoneList());

        //找按钮,设置点击事件
        Button button = (Button) findViewById(R.id.bt);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**调用P层方法*/
                mPhonePresenter.addPhone(new Phone("nokia",3000+ new Random().nextInt(1000)));
            }
        });

        //设置适配器并设置item的点击事件
        mListView.setAdapter(mArrayAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mPhonePresenter.removePhone(position);
            }
        });
    }

    @Override
    public void showCreatingPhone() {
        //创建进度dialog对象
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setMessage("工厂正在生产手机");
        mProgressDialog.setCancelable(true);
        mProgressDialog.show();
    }

    @Override
    public void showPhoneCountChange() {
        mArrayAdapter.notifyDataSetChanged();
    }

    @Override
    public void showNoPhone() {
        //没有手机时设置文本显示状态
        findViewById(R.id.noPhone).setVisibility(View.VISIBLE);
    }

    @Override
    public void showFactoryBusy() {
        showToast("工厂繁忙，请稍后再试!");
    }

    @Override
    public void showCreatedPhone() {
        if (mProgressDialog != null && mProgressDialog.isShowing()){
            mProgressDialog.dismiss();
        }else {
            mProgressDialog =null;
            showToast("新生产一台手机");
            findViewById(R.id.noPhone).setVisibility(View.VISIBLE);
        }
    }

    private void showToast(String text){
        if (mToast==null){
            mToast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
        }else {
            mToast.setText(text);
        }
        mToast.show();
    }
}
